package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author：YH
 * @Description：
 * @Date:Created in 2021/4/30 18:42
 */
@RestController
@Slf4j
public class PaymentControler {

    @Resource
    private PaymentService paymentService;

    //服务生产者 但是我订单调用的时候不知道调用的服务里面的那个因此需要增加信息

    @Value("${server.port}")
    private String serverPort;//根据提供的服务进行负载均衡的查找，默认应该是采用轮询的算法

    @Resource
    private DiscoveryClient discoveryClient;

    //发送请求
    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody Payment payment) {
        log.info("-------------查询结果:[{}]", payment);
        log.info("-------------服务端口号为:[{}]", serverPort);
        int result = paymentService.create(payment);
        log.info("-------------插入结果:[{}]", result);
        if (result > 0) {
            log.info("插入成功" + result);
            return new CommonResult(200, "插入数据库成功" + " 端口号为" + serverPort + "结果为" + result, payment);
        } else {
            log.info("插入失败" + result);
            return new CommonResult(444, "插入数据库失败", result);
        }
    }

    // order模块调用地址 http://CLOUD-PAYMENT-SERVICE/payment/get/1
    //读取请求
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("-------------查询结果:[{}]", payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功,id号" + id + "端口号为" + serverPort, payment);
        } else {
            return new CommonResult(444, "没有对应记录,查询 ID: " + id, null);
        }
    }

    @GetMapping(value = "/payment/discovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        for (String s : services) {
            log.info("element" + s);
        }

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance s : instances) {
            log.info(s.getServiceId() + "\t" + s.getInstanceId() + "\t" + s.getHost() + "\t" + s.getPort() + "\t" + s.getUri());
        }
        return this.discoveryClient;
    }


     // 调用地址   http://169.254.232.83:8001/payment/lb
    // http://CLOUD-PAYMENT-SERVICE/payment/lb

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB() {
        return serverPort;//传输端口号实现自定义的负载轮询算法
    }

    /**
     * feign超时测试
     *
     * @return
     */
    @GetMapping("/payment/feign/timeout")
    public String paymentFeignTimeOut() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }
}
